<?php
/**
 * 档案管理
 * @since   2018-06-17
 * @author  winljm001 <winljm001@126.com>
 */

namespace app\admin\controller;



use app\model\AdminExaminationRoom;
use app\model\AdminTestPersonnel;
use app\model\AdminTestPaper;
use app\model\AdminAcademicRecords;
use app\util\ReturnCode;
use app\util\Tools;

class AcademicRecords extends Base {
    /**
     * 考场成绩列表
     * @return array
     * @throws \think\exception\DbException
     * @author  winljm001 <winljm001@126.com>
     */
    public function index() {
        $limit = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $testStatus = $this->request->get('testStatus', '');
        $isQualified = $this->request->get('isQualified', '');
        $keywords = $this->request->get('keywords', '');
        $keywords2 = $this->request->get('keywords2', '');
        $gradeGt = $this->request->get('gradeGt', '-9999');
        $gradeLt = $this->request->get('gradeLt', '9999');
        $eRoomId = $this->request->get('eRoomId');
        $where = [];
        $where['e.status'] = 1;
        $where['h.grade'] = ['between', $gradeGt.','.$gradeLt];
        if ($eRoomId) {
            $where['a.examinationRoomId'] = $eRoomId;
        }
        if($testStatus!=''){
            if($testStatus==1){
                $testStatusQStr = '((f.examEndTime>="'.time().'") OR (f.examEndTime<"'.time().'" AND h.grade>0))';
            }else{
                $testStatusQStr = '(f.examEndTime<"'.time().'" AND h.grade<0)';
            }
        }else{
            $testStatusQStr='';
        }
        if($isQualified!=''){
            if($isQualified=='合格'){
                $isQualifiedQStr='(h.grade>=g.passScore)';
            }else{
                $isQualifiedQStr='(h.grade<g.passScore)';
            }
        }else{
            $isQualifiedQStr='';
        }
        if($keywords!=''){
            $where=array_merge($where,['e.id|e.name|e.idcard|e.phone'=>['like', "%{$keywords}%"]]);
        }
        if($keywords2!=''){
            $where=array_merge($where,['f.name|g.name'=>['like', "%{$keywords2}%"]]);
        }
        $listObj=(new AdminTestPersonnel())->alias('a')
        ->join('admin_examinee e','a.examineeId = e.id')
        ->join('admin_examination_room f','a.examinationRoomId = f.id')
        ->join('admin_test_paper g','f.testPaperId = g.id')
        ->join('admin_academic_records h','h.testPersonnelId = a.id')
        ->field('e.*,f.name as eRoomName,g.name as testPaperName,a.id as testPersonnelId,f.examStartTime,f.examEndTime,f.isPigeonhole,g.passScore,h.grade,h.id as academicRecordsId,h.StartTime as eStartTime')
        ->where($where)->where($testStatusQStr)->where($isQualifiedQStr)->order('a.updateTime DESC')
        ->paginate($limit, false, ['page' => $start])->toArray();
        $listInfo = $listObj['data'];
        // var_dump($where);
        $where3=[];
        foreach ($listInfo as $key => $value) {
            $isUnsetFlag=false;
            $listInfo[$key]['testStatus'] = ($value['examEndTime']<time()&&$listInfo[$key]['grade']==-1)?0:1;
            if($value['examEndTime']<time()){
                $listInfo[$key]['isQualified']=$listInfo[$key]['grade']>=$listInfo[$key]['passScore']?"合格":"不合格";
            }else{
                $listInfo[$key]['isQualified']="考试未开始";
            }
        }
        // var_dump($testPersonnelList);
        return $this->buildSuccess([
            'list'  => $listInfo,
            'count' => $listObj['total']
        ]);
    }

    /**
     * 新增成绩记录
     * @return array
     * @author  winljm001 <winljm001@126.com>
     */
    public function add() {
        $postData = $this->request->post();
        $res = AdminAcademicRecords::create($postData);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
        } else {
            return $this->buildSuccess([]);
        }
    }

    /**
     * 删除成绩记录
     * @return array
     * @author winljm001 <winljm001@126.com>
     */
    public function del() {
        $id = $this->request->get('id');
        if (!$id) {
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
        }
        AdminAcademicRecords::destroy($id);
        return $this->buildSuccess([]);
    }

    /**
     * 获取成绩记录
     * @return object
     * @author winljm001 <winljm001@126.com>
     */
    public function getDetail() {
        $id = $this->request->get('id');
        if (!$id) {
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
        }
        $where=[];
        $where['a.id']=$id;
        $data=(new AdminAcademicRecords())->alias('a')
        ->join('admin_test_personnel b','a.testPersonnelId = b.id')
        ->join('admin_examinee c','b.examineeId = c.id')
        ->join('admin_examination_room f','b.examinationRoomId = f.id')
        ->join('admin_test_paper g','f.testPaperId = g.id')
        ->where($where)
        ->field('a.*,b.*,c.name as examineeName,c.idcard,f.name as examinationRoomName,g.name as testPaperName,g.totalPoints,g.passScore,g.duration')
        ->find();
        return $this->buildSuccess([
            'academicRecords'  => $data
        ]);
    }

    /**
     * 编辑成绩记录
     * @author winljm001 <winljm001@126.com>
     * @return array
     * @throws \think\exception\DbException
     */
    public function edit() {
        $postData = $this->request->post();
        $res = AcademicRecords::update($postData);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
        } else {
            return $this->buildSuccess([]);
        }
    }

    /**
     * 成绩记录状态编辑
     * @return array
     * @author winljm001 <winljm001@126.com>
     */
    // public function changeStatus() {
    //     $idArr = explode(',',$this->request->get('id'));
    //     if ($idArr[0]== '') {
    //         return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '缺少必要参数');
    //     }
    //     $status = $this->request->get('status');
    //     foreach ($idArr as $key => $value) {
    //         $res = AcademicRecords::update([
    //             'Id'         => $value,
    //             'status'     => $status
    //         ]);
    //     }
    //     if ($res === false) {
    //         return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
    //     } else {
    //         return $this->buildSuccess([]);
    //     }
    // }
    
}
